package org.wheel.plugins.toolkit.sequence;

import org.wheel.plugins.toolkit.cache.spi.CacheService;
import org.wheel.plugins.toolkit.cache.spi.LockProvider;

public class CacheLockProvider implements LockProvider {

    private final CacheService cacheService;

    public CacheLockProvider(CacheService cacheService) {
        this.cacheService = cacheService;
    }

    @Override
    public boolean tryLock(String key, long expireSeconds) {
        return Boolean.TRUE.equals(cacheService.setNx(key, "1", expireSeconds));
    }

    @Override
    public void unlock(String key) {
        cacheService.removeByKey(key);
    }
}
